import numpy as np
import pandas as pd
import sys
import cv2
sys.path.append(".")
def get_2_sigma(result,dims=1):
    '''
    dims== None means the result only has one dim
    :param result:
    :param dims:
    :return:
    '''
    if dims !=1:
        static_average = np.average(result[1:, :], axis=0)
        static_median = np.median(result[1:, :], axis=0)
        static_stv = np.std(result[1:, :], axis=0)
        static_upper = static_average + 2 * static_stv
        static_lower = static_average - 2 * static_stv
        upper_index = result > static_upper
        lower_index = result < static_lower
        final_index = np.argwhere((upper_index + lower_index) == True)
        for index in final_index:
            #print(index,result[index[0], index[1]],static_average[index[1]])
            result[index[0], index[1]] = static_median[index[1]]
    else:
        static_average = np.average(result[1:], axis=0)
        median_value = np.median(result[1:], axis=0)
        static_stv = np.std(result[1:], axis=0)
        #print(static_stv)
        static_upper = static_average + 2 * static_stv
        static_lower = static_average - 2 * static_stv
        upper_index = result > static_upper
        lower_index = result < static_lower
        final_index = np.argwhere((upper_index + lower_index) == True)
        #final_index = np.argwhere((upper_index.values + lower_index.values) == True)
        result = np.array(result)
        #print(final_index,result[55])
        for index in final_index:
            #print(index[0],result[index[0]],static_average)
            result[index[0]] = median_value
    #print("=============")
    return result
'''
data = pd.read_excel("../model_latency/resnet_v2_sequency_latency.xlsx",index_col=0)
temp = data.iloc[1:,:].values
result = get_2_sigma(temp,4)
result = pd.DataFrame(data = result,index = range(result.shape[0]),columns=[data.columns])
result.to_excel("../model_latency/resnet_v2_sequency_latency_2sigma.xlsx")
'''


def read_image(filename, resize_height, resize_width, normalization=True):
    '''
    读取图片数据,默认返回的是uint8,[0,255]
    :param filename:
    :param resize_height:
    :param resize_width:
    :param normalization:是否归一化到[0.,1.0]
    :return: 返回的图片数据
    '''

    bgr_image = cv2.imread("./middle_data/" + filename)
    print(bgr_image.shape)
    if len(bgr_image.shape) == 2:  # 若是灰度图则转为三通道
        print("Warning:gray image", filename)
        bgr_image = cv2.cvtColor(bgr_image, cv2.COLOR_GRAY2BGR)

    rgb_image = cv2.cvtColor(bgr_image, cv2.COLOR_BGR2RGB)  # 将BGR转为RGB
    # show_image(filename,rgb_image)
    # rgb_image=Image.open(filename)
    if resize_height > 0 and resize_width > 0:
        rgb_image = cv2.resize(rgb_image, (resize_width, resize_height))
    rgb_image = np.asanyarray(rgb_image)
    if normalization:
        # 不能写成:rgb_image=rgb_image/255
        rgb_image = rgb_image / 255.0
        # rgb_image = rgb_image / 255.0
    rgb_image = np.asarray(rgb_image, dtype=np.float32)
    return rgb_image